package cn.qiyu5522.controller;


import cn.qiyu5522.domain.entity.ResponseResult;
import cn.qiyu5522.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author Tonkey
 * @since 2024-04-09
 */
@Api(tags = "文章相关接口")
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;


    @ApiOperation(value = "热门文章",notes = "按阅读量给文章进行排序")
    @GetMapping("/hotArticleList")
    public ResponseResult hotArticleList() {
        // 查询热门文章, 封装 ResponseResult 返回
        return articleService.hotArticleList();
    }


    @ApiOperation(value = "分页查询文章",notes = "分页查询文章某分类ID下的文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页号"),
            @ApiImplicitParam(name = "pageSize",value = "每页大小"),
            @ApiImplicitParam(name = "categoryId", value = "分类编号")
    }
    )
    @GetMapping({"/articleList/{pageNum}/{pageSize}/{categoryId}", "/articleList/{pageNum}/{pageSize}"})
    public ResponseResult articleListWithPage(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @PathVariable(value = "categoryId", required = false) Long categoryId) {
        return articleService.articleListWithPage(pageNum, pageSize, categoryId);
    }


    @ApiOperation(value = "根据ID查询文章",notes = "根据文章ID查询一篇文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章编号")
    })
    @GetMapping("/{id}")
    public ResponseResult article(@PathVariable Long id) {
        return articleService.getArticleById(id);
    }

    @ApiOperation(value = "更新文章阅读量",notes = "根据文章ID更新文章阅读量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章编号")
    })
    @PutMapping("/updateViewCount/{id}")
    public ResponseResult updateViewCount(@PathVariable Long id){
        return articleService.updateViewCount(id);
    }




}
